/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			CustMdl.h
  \date			June 2011
  \author		TNick

  \brief		Contains the definition for CustMdl class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __CUSTMDL_INC__
#define __CUSTMDL_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QAbstractItemModel>

#include	<generic/cc_l2d.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/// class
/**
*
*/
class CustMdl : public QAbstractItemModel		{

	//
	//
	//
	//
	/*  DEFINITIONS    ----------------------------------------------------- */

	/*  DEFINITIONS    ===================================================== */
	//
	//
	//
	//
	/*  DATA    ------------------------------------------------------------ */

protected:

	/// tell if this model is editable or not
	/**
*	editable models store copies of the items in list and delete the chain
*	when the constructor exits; models that are not editable simply copy the
*	elements in provided header and use the elements from that chain.
*/
	bool				b_editable;

	/// header for list of items that follows
	CC_HDL2D			itm_lst;

	/*  DATA    ============================================================ */
	//
	//
	//
	//
	/*  FUNCTIONS    ------------------------------------------------------- */

public:

	/// constructor;
	CustMdl				(
		QObject*		parent = NULL,	/**< the owner */
		CC_HDL2D*		hd_src = NULL,	/**< from where to copy */
		bool			b_copy = true	/**< tell if the constructor will
	  use the existing tree from CC_App (which will make this model read-only)
	  or will leave it empty (but modifiable) */
		);

	/// destructor;
	virtual				~CustMdl		(void) {}


	/* alow fellows to trigger change signal */
	inline void			dataHasChanged(QModelIndex the_idx)
	{ emit dataChanged(the_idx, the_idx); }
	inline void			dataHasChanged(QModelIndex the_idx1, QModelIndex the_idx2)
	{ emit dataChanged(the_idx1, the_idx2); }
	/* alow fellows to create indexes */
	inline QModelIndex	createIndex(int row, int col, void* p) const
	{ return QAbstractItemModel::createIndex(row, col, p); }

	inline bool			isEditable(void) const
	{ return b_editable; }

	/// after a new item is appended this function shall be used
	inline void			sigTbAppended	(CC_L2D* pItem)
	{
		itm_lst.append(pItem);
		QModelIndex the_idx = createIndex(itm_lst.count()-1, 0, pItem);
		emit dataChanged(the_idx, the_idx);
	}

	/// get first kid
	inline CC_L2D*	firstKid(void) {
		return static_cast<CC_L2D*>(itm_lst.first());	}

	/// get n'th kid
	inline CC_L2D*	nthKid(int n) {
		return static_cast<CC_L2D*>(itm_lst.item(n)); }

	/// append an item to this item's chain
	inline void		append(CC_L2D* a_it)
	{
		if (b_editable)
			itm_lst.append(a_it);
	}


protected:

	/// creates a proper new item
	/**
*	The function shall not insert this new member
*	inside it's chain. Also
*/
	virtual CC_L2D*	new_item(void) = 0;

public:
	/// duplicate the chains for actions, menus and toolbars
	/**
*	This function is indended to provide a fully duplicated environment
*	where changes can be made but original state may also be reverted.
*/
	static bool		duplicate_chains(
		CustMdl*		p_actm,	/**< action header that recives new items*/
		CustMdl*		p_tlbm,	/**< toolbar header that recives new items*/
		CustMdl*		p_mnum	/**< menu header that recives new items*/
	);

protected:

	/* QAbstractItemModel */

	QModelIndex		index					(
		int						row,
		int						column,
		const					QModelIndex & parent = QModelIndex()
		) const;

	Qt::ItemFlags	flags					(
		const QModelIndex &		index
		) const
	{ Q_UNUSED(index);
		if (isEditable())
			return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
		else
			return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
	}

	int				rowCount				(
		const QModelIndex &		parent = QModelIndex()
		) const
	{	Q_UNUSED(parent);
		if (parent.isValid())
			return 0;
		return itm_lst.count();
	}

	bool			removeRows				(
		int						row,
		int						count,
		const QModelIndex &		parent = QModelIndex()
		);

	bool			insertRows				(
		int						row,
		int						count,
		const QModelIndex &		midx);

	bool			removeColumns			(
		int						column,
		int						count,
		const QModelIndex &		parent = QModelIndex()
		){
		Q_UNUSED(column);
		Q_UNUSED(count);
		Q_UNUSED(parent);

		return false;
	}

	bool			insertColumns			(
		int						column,
		int						count,
		const QModelIndex &		parent = QModelIndex()
		){
		Q_UNUSED(column);
		Q_UNUSED(count);
		Q_UNUSED(parent);

		return false;
	}

	QModelIndex		parent (
		const QModelIndex &		index
		) const
	{
		Q_UNUSED(index);
		return QModelIndex();
	}



	/*  FUNCTIONS    ======================================================= */
	//
	//
	//
	//

};	/*	class CustMdl	*/

/*  CLASS    =============================================================== */
//
//
//
//
#endif // __CUSTMDL_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */

